home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 140 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.9 KB  |  92 lines

  1. Path: fido.asd.sgi.com!austern
  2. From: kanze@gabi.gabi-soft.fr (J. Kanze)
  3. Newsgroups: comp.std.c++
  4. Subject: Guarantees concerning references/pointers into string
  5. Date: 25 Jan 1996 09:46:59 PST
  6. Organization: GABI Software, Sarl.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <KANZE.96Jan25184235@gabi.gabi-soft.fr>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. X-Original-Date: 25 Jan 1996 17:42:35 GMT
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBVAwUBMQfCP0y4NqrwXLNJAQGzgAIAw5JsKqmY9ANtlWtE9hjaWzvQMN+qo8x4
  13.     jL9LDjY6T2vR92X71vWwEdkIzOWy3nvVYAlBNbfIEOZTez56bYWPlg==
  14.     =5G9Y
  15. Originator: austern@isolde.mti.sgi.com
  16.  
  17. I've just tried out the following code:
  18.  
  19.     #include    <string>
  20.     #include    <iostream.h>
  21.  
  22.     int
  23.     main()
  24.     {
  25.         string              a , b ;
  26.         a = "A lot of junk" ;
  27.         a.reserve( 100 ) ;
  28.         char*               p1 = &a[ 1 ] ;
  29.         //  b = a ;
  30.         a.insert( 6 , "---" ) ;
  31.         char*               p2 = &a[ 1 ] ;
  32.         cout << "Reserve test: " << (p1 == p2 ? "Passed" : "Failed") << endl ;
  33.         return 0 ;
  34.     }
  35.  
  36. With the one `standard' string class I have access to, the result is
  37. "Failed".
  38.  
  39. As I interpret the standard, the above is required to work.  The
  40. `Notes' after the function `reserve' state that: ``It is guaranteed
  41. that no reallocation takes place during the insertions that happen
  42. after reserve() takes place till the time when the size of the string
  43. reaches the size specified by reserver().''  From further comments
  44. concerning reallocation, I would conclude that the results of
  45. operator[] should be valid until a reallocation takes place.  Is this
  46. the intended interpretation?
  47.  
  48. Just a guess, but I would imagine from the above, even without seeing
  49. the g++ code, that it uses the widespread copy on write technique.  My
  50. own implementation of basic_string< T > uses copy on write, and it was
  51. the amount of effort required to avoid the above problem that suggested
  52. trying it out on someone elses implementation.  The presense of
  53. `reserve' makes copy on write anything but trivial to implement
  54. correctly.  (The exact case above is actually easy to avoid, and I'm
  55. surprised that g++ has the error.  But try uncommenting the assignment
  56. in the above code, and it becomes considerably more difficult to avoid
  57. the problem.)
  58.  
  59. In a similar veine, what functions are implied by the word `insertion'
  60. in the above quote?  I chose `insert' for my test, because if it isn't
  61. an insertion, I don't know what is.  What about replace?  remove?
  62. assign?  non-const at?
  63.  
  64. One further question occurs: when may reallocation (and the resulting
  65. invalidation of the pointers) occur when reserve has not been called?
  66. For example, is the following guaranteed to work as expected:
  67.  
  68.     string              s ;
  69.     size_t              i , j ;
  70.     assert( i < s.size() , j < s.size() ) ;
  71.     a[ i ] = a[ j ] ;
  72.  
  73. I would like for the last statement to be well defined, but according to
  74. my reading of the standard, it isn't.  Reallocation is allowed in the
  75. non-const version of operator[] (and must be, if copy on write is to be
  76. a legal implementation).  This operator is called twice in the
  77. expression, and the compiler could very easily (and legally) generate
  78. both calls before using either of the results.  But if the second call
  79. reallocates, the reference returned by the first call is invalidated.
  80. (I cannot actually conceive of an implementation in which the second
  81. call reallocates, but I cannot find anything in the draft to guarantee
  82. that it won't.)
  83. -- 
  84. James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
  85. GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
  86. Conseils, itudes et rialisations en logiciel orienti objet --
  87.               -- A la recherche d'une activiti dans une region francophone
  88. ---
  89. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  90.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  91.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  92.